﻿using DotNetCommon.Extensions;
using System.Linq;

namespace DBUtil.Provider.MySql;

/// <summary>
/// DBAccess的扩展方法
/// </summary>
public static class DBAccessExtensions
{
    #region IsMySql
    /// <summary>
    /// 是否是MySql数据库
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static bool IsMySql(this DBAccess db)
    {
        return db.DBType == EnumDBType.MySql;
    }
    #endregion

    #region MySql版本判断
    /*
    可能版本字符串列表
    5.7.25-log
    5.7.32-log
    8.0.16
    8.0.25        
     */

    private static (int first, int second, int third) ParseVersion(string version)
    {
        var arr = version.IfNullUse("").SplitAndTrimTo<string>(".");
        if (arr.Count < 3)
        {
            return (0, 0, 0);
        }
        arr = arr.Take(3).ToList();
        if (int.TryParse(arr[0], out int first))
        {
            if (int.TryParse(arr[1], out int second))
            {
                var ints = "";
                foreach (var i in arr[2])
                {
                    if ((int)i >= (int)'0' && (int)i <= (int)'9')
                    {
                        ints += i;
                        continue;
                    }
                    break;
                }
                if (int.TryParse(ints, out int third))
                {
                    return (first, second, third);
                }
            }
        }
        return (0, 0, 0);
    }

    /// <summary>
    /// 是否兼容MySql8
    /// </summary>
    /// <returns></returns>
    public static bool IsMySql8Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.MySql && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.Mysql8Compatible;
        var (first, second, third) = ParseVersion(db.DBVersion);
        if (first >= 8) db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.Mysql8Compatible);
        return first >= 8;
    }

    ///// <summary>
    ///// 是否兼容指定版本的MySql
    ///// </summary>
    ///// <param name="db"></param>
    ///// <param name="versionNum">版本号（如: 8.0.25、5.7.26、5.7）</param>
    ///// <returns></returns>
    //public static bool IsMySqlCompatible(this DBAccess db, string versionNum)
    //{
    //    var (first1, second1, third1) = ParseVersion(versionNum);
    //    var (first2, second2, third2) = ParseVersion(db.DBVersion);
    //    if (first2 < first1) return false;
    //    if (first1 == first2 && second2 < second1) return false;
    //    if (first1 == first2 && second2 == second1 && third2 < third1) return false;
    //    return true;
    //}
    #endregion

    #region db行为配置
    /// <summary>
    /// 是否认为 mysql 开启了sql_model: NO_BACKSLASH_ESCAPES(一般 mysql 默认不开启)<br />
    /// 如果开启后, 则反斜杠将不是转义符, 且单引号包裹的字符串可以直接换行(这更符合sql标准, 和 sqlite、sqlserver 表现一致)
    /// </summary>
    /// <remarks>
    /// 参考: <seealso href="https://blog.csdn.net/u010476739/article/details/122959915"/>
    /// </remarks>
    public static DBSetting SetMySqlBehavior_SqlMode_NO_BACKSLASH_ESCAPES(this DBSetting setting, bool isDBEnable = false)
    {
        return setting.AddExtendSetting(Consts.SqlMode_NO_BACKSLASH_ESCAPES, isDBEnable);
    }
    /// <summary>
    /// 获取创建 DBAccess 是是否配置了 DBSetting.SetMySqlBehavior_SqlMode_NO_BACKSLASH_ESCAPES(true)
    /// </summary>
    /// <remarks>
    /// 参考: <seealso href="https://blog.csdn.net/u010476739/article/details/122959915"/>
    /// </remarks>
    public static bool IsMySqlBehavior_SqlMode_NO_BACKSLASH_ESCAPES(this DBAccess db)
    {
        if (db.TryGetExtendSetting<bool>(Consts.SqlMode_NO_BACKSLASH_ESCAPES, out var flag))
        {
            return flag;
        }
        return false;
    }
    #endregion
}
